<template>
	<view class="time">
		{{data}}
	</view>
</template>

<script>
	export default {
		props:{
			date:{
				type:String,
				value:null,
			}
		},
		data() {
			return {
				endDate:this.date,
				endTime:true,
				data:''
			}
		},
		mounted() {
			this.countTime()
		},
		methods: {
			//倒计时
			countTime() {
				var date = new Date();
				var now = date.getTime();
				//设置截止时间
				var endDate = new Date(this.endDate);
				var end = endDate.getTime();
				//时间差
				var leftTime = end - now;
				leftTime = leftTime - 1000
				if (leftTime >= 0) {
					this.endTime = false
					let d = Math.floor(leftTime / 1000 / 60 / 60 / 24);
					let h = Math.floor(leftTime / 1000 / 60 / 60 % 24);
					let m = Math.floor(leftTime / 1000 / 60 % 60);
					let s = Math.floor(leftTime / 1000 % 60);
					if (d <= 9) {
						d = `0${d}`
					}
					if (h <= 9) {
						h = `0${h}`
					}
					if (m <= 9) {
						m = `0${m}`
					}
					if (s <= 9) {
						s = `0${s}`
					}
					this.data = m + ' 分 ' + s + ' 秒 '
					setTimeout(this.countTime, 1000);
				} else{
					this.endTime = true
				}
			},
		},
		watch:{
			date:{
				handler(val) {
					//console.log(val)
					this.endDate = val
					this.countTime()
					
				}
			},
			endTime(newV, oldV) { 
				//console.log(newV)
				this.$emit('endTime', {
					endTime:newV
				})
			}
		}
	}
</script>

<style scoped>
	.time{
		font-size: 16px;
		font-weight: bold;
		color:#FFFFFF
	}
</style>
